<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('图书列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>图书名称：</label>
                                <input type="text" name="name"/>
                            </li>
                            <li>
                                <label>图书编号：</label>
                                <input type="text" name="bookNo"/>
                            </li>
                            <li>
                                图书类型：<select name="typeId" id="selectType">
                                <option value="">所有</option>
                            </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="restore()" shiro:hasPermission="admin:book:edit">
                    <i class="fa fa-plus"></i> 还原
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="admin:book:remove">
                    <i class="fa fa-remove"></i> 永久删除
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('admin:book:edit')}]];
        var removeFlag = [[${@permission.hasPermi('admin:book:remove')}]];
        var prefix = ctx + "admin/book";

        $(function() {

            var options = {
                url: prefix + "/listWithTypeDel",
                removeUrl: prefix + "/removeForever",
                modalName: "图书",
                columns: [{
                    checkbox: true,
                    visible: editFlag == 'hidden' ? false : true
                },
                {
                    field: 'bookId',
                    title: '图书id',
                    visible: false
                },
                {
                    field: 'bookNo',
                    title: '图书编号'
                },
                {
                    field: 'name',
                    title: '名称'
                },
                {
                    field: 'bookType.name',
                    title: '类型'
                },
                {
                    field: 'images',
                    title: '图片',
                    formatter: function(value, row, index) {
                        return $.table.imageView(row.images);
                    }
                },
                {
                    field: 'introduce',
                    title: '介绍'
                },
                {
                    field: 'price',
                    title: '价格',
                    formatter: function(value, row, index) {
                        if(row.price!=null){
                            var actions = [];
                            actions.push('&yen; '+row.price);
                            return actions.join('');
                        }
                    }
                },
                {
                    visible: editFlag == 'hidden' ? false : true,
                    field: 'status',
                    title: '状态',
                    formatter: function (value, row, index) {
                        return statusTools(row);
                    }
                },
                {
                    field: 'loanStatus',
                    title: '是否借出',
                    formatter: function (value, row, index) {
                        if (row.loanStatus == 1) {
                            return '未借出';
                        } else {
                            return '已借出';
                        }
                    },
                    cellStyle: function(value,row,index){
                        if (row.loanStatus == 1) {
                            return {
                                css:{
                                    'color':'#008018'
                                }
                            };
                        } else {
                            return {
                                css:{
                                    'color':'#ad0404'
                                }
                            };
                        }
                    }
                },
                {
                    visible: editFlag == 'hidden' ? false : true,
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="recycle(\'' + row.bookId + '\')"><i class="fa fa-edit"></i>还原 </a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.bookId + '\')"><i class="fa fa-remove"></i>永久删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);

            //类型下拉框初始化
            let view = $('#selectType');
            $.ajax({
                url:ctx + 'admin/type/listNotPage',
                type:'post',
                dataType:'json',
                success: function(result) {
                    result.rows.map( item => {
                        view.append('<option value="'+ item.typeId +'">'+ item.name +'</option>');
                    });
                }
            });
        });

        /* 图书状态显示 */
        function statusTools(row) {
            if (row.status == 2) {
                return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enable(\'' + row.bookId + '\')"></i> ';
            } else {
                return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disable(\'' + row.bookId + '\')"></i> ';
            }
        }

        /* 图书管理-停用 */
        function disable(bookId) {
            $.modal.confirm("确认要停用图书吗？", function() {
                $.operate.post(prefix + "/changeStatus", { "bookId": bookId, "status": 2 });
            })
        }

        /* 图书管理-启用 */
        function enable(bookId) {
            $.modal.confirm("确认要启用图书吗？", function() {
                $.operate.post(prefix + "/changeStatus", { "bookId": bookId, "status": 1 });
            })
        }

        function recycle(id){
            $.modal.confirm("确认要还原吗?", function() {
                $.ajax({
                    url:prefix + '/batchRestore',
                    type:'post',
                    data:{ids:id},
                    dataType:'json',
                    beforeSend: function () {
                        $.modal.loading("正在处理中，请稍候...");
                        $.modal.disable();
                    },
                    success: function(result) {
                        if (typeof callback == "function") {
                            callback(result);
                        }
                        $.operate.ajaxSuccess(result);
                    }
                });
            });
        }

        function restore() {
            table.set();
            var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
            if (rows.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            var ids = rows.join();
            $.modal.confirm("确认要还原选中的" + rows.length + "条数据吗?", function() {
                $.ajax({
                    url:prefix + '/batchRestore',
                    type:'post',
                    data:{ids},
                    dataType:'json',
                    beforeSend: function () {
                        $.modal.loading("正在处理中，请稍候...");
                        $.modal.disable();
                    },
                    success: function(result) {
                        if (typeof callback == "function") {
                            callback(result);
                        }
                        $.operate.ajaxSuccess(result);
                    }
                });
            });

        }
    </script>
</body>
</html>